/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.saving;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import src.domain.saving.Saving;
import src.domain.saving.Savtrx;
import src.domain.security.Users;
import src.domain.system.Numbering;
import src.services.CRUEDService;
import src.services.saving.SavingModel;
import src.services.saving.SavtrxModel;
import src.util.Messager;
import src.view.security.LoginBean;
import src.view.system.ApprovalBean;

/**
 *
 * @author jasuke
 */
@ManagedBean
@SessionScoped
public class SavtrxApprovalBean {

    private Savtrx savtrx;  
    private Saving savdest;  
    private List<Savtrx> savtrxs;  
    private String accntDest;
    private SavtrxModel savHelper;
    private boolean editMode = true;
    private BigDecimal balanceDest = BigDecimal.ZERO, balanceSrc = BigDecimal.ZERO;
    private Users users;
    private ApprovalBean approvalBean;
    
    public SavtrxApprovalBean() {
        ambilUser();
        this.savHelper = new SavtrxModel();
        this.savtrxs = new ArrayList<>();
        FacesContext context = FacesContext.getCurrentInstance();
        approvalBean = (ApprovalBean) context.getApplication().evaluateExpressionGet(context, "#{approvalBean}", ApprovalBean.class);
        approvalBean.setUpdateTab(false);
    }

    public String getAccntDest() {
        return accntDest;
    }

    public BigDecimal getBalanceDest() {
        return balanceDest;
    }

    public BigDecimal getBalanceSrc() {
        return balanceSrc;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public Saving getSavdest() {
        return savdest;
    }

    public List<Savtrx> getSavtrxs() {
        return savtrxs;
    }

    public Savtrx getSavtrx() {
        return savtrx;
    }

    public void setSavtrx(Savtrx savtrx) {
        this.savtrx = savtrx;
    }
    
    public void calcSaldoAhir() {
        if (savtrx.getAmount() != null) {
            this.balanceSrc = savtrx.getSaving().getBalance().subtract(savtrx.getAmount());
            this.balanceDest = savdest.getBalance().add(savtrx.getAmount());
        }    
    }
    
    public final void loadSavtrxs() {
        Map<String, Object> savfilter = new HashMap<>();
        Map<Object, Object> amtfilter = new HashMap<>();
        amtfilter.put("<=", users.getUgroup().getSavMaxTrx());
        savfilter.put("approvalUid", null);
        savfilter.put("amount", amtfilter);
        
        this.savtrxs = savHelper.findAll(savfilter, " order by transDate");
    }
    
    private void initForm() {
        this.editMode = true;
        this.savdest = new Saving();
        this.accntDest="";
        balanceDest = BigDecimal.ZERO;
        balanceSrc = BigDecimal.ZERO;
    }
    
    public void openData() {
        approvalBean.setUpdateTab(false);
        initForm();
        savdest = new SavingModel().getById(savtrx.getDestSavaccntId());
        if(savdest != null) this.accntDest = savdest.getAccountNo();
        if (savtrx.getAmount() != null) calcSaldoAhir();
    }
        
    public void approve() {
        RequestContext context = RequestContext.getCurrentInstance();
        
        Numbering currentNo = new CRUEDService().getNextTrxNumber(2, savtrx.getBranchId(), 0, 1);
        savtrx.setTransferNo(currentNo.getTrxNum());
            
        //men-triger history and balance
        savtrx.setPosted(true);
        savtrx.setApprovalUid(users.getUserId());
        boolean success = savHelper.saveData(savtrx);
        
        if (success) {
            this.savtrxs.remove(this.savtrx);
            approvalBean.updateTabOtor();
            //this.savtrx = null;
        } 
        
        Messager.showSaveUpdateStatus(success);     
        context.addCallbackParam("success", success);  
    }
    
    private void ambilUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean logBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        this.users = logBean.getSelectedUser();
    }
    
     public void handleClose() {
        this.savtrx = null;
    }
}
